package com.auotc.drawerlayoutdemo

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentTabHost
import android.support.v4.widget.DrawerLayout
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import kotlinx.android.synthetic.main.fragment_main2.*
import java.util.ArrayList

/**
 * Created by lgs on 2019/4/13.
 */
class MainFragment : Fragment() {

    private var mInflater: LayoutInflater? = null
    private var mTabHost: FragmentTabHost? = null
    private val tabDataList = ArrayList<TabDataBean>(5)
    private lateinit var framents: MutableList<Fragment>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        mInflater = LayoutInflater.from(activity)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_main2, container, false)
//        mTabHost = view.findViewById(android.R.id.tabhost)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        initTabHost()
        initFragment()
    }

    /**
     * 初始化底部导航栏
     */
    private fun initTabHost() {
        //初始化fTabHost, 第三个参数为内容容器
        mTabHost!!.setup(context, childFragmentManager, R.id.realtabcontent)
        /*初始化数据源*/
        val tabHome = TabDataBean(R.string.tabHome, R.drawable.selector_tab_home, HomeFragment::class.java)
        val tabHot = TabDataBean(R.string.tabHot, R.drawable.selector_tab_hot, HotFragment::class.java)
        val tabCategory = TabDataBean(R.string.tabCategory, R.drawable.selector_tab_category, CategoryFragment::class.java)
        val tabCart = TabDataBean(R.string.tabCart, R.drawable.selector_tab_cart, CartFragment::class.java)
        tabDataList.add(tabHome)
        tabDataList.add(tabHot)
        tabDataList.add(tabCategory)
        tabDataList.add(tabCart)
        //添加底部菜单项-tabSpec
        for (bean in tabDataList) {
            val tabSpec = mTabHost!!.newTabSpec(getString(bean.getTabName()))
            //给菜单项添加内容，indicator,其中indicator需要的参数View即为菜单项的布局
            tabSpec.setIndicator(getIndicatorView(bean))
            //第二参数就是该菜单项对应的页面内容
            mTabHost!!.addTab(tabSpec, bean.content, null)
        }
    }

    private fun getIndicatorView(bean: TabDataBean): View {
        val view = mInflater!!.inflate(R.layout.tab_indicator, null)
        val iconTab = view.findViewById(R.id.iv_tab_icon) as ImageView
        val tvTab = view.findViewById(R.id.tv_tab_text) as TextView
        iconTab.setImageResource(bean.tabIcon)
        tvTab.setText(bean.tabName)
        return view
    }

    private fun initFragment() {
        framents = mutableListOf(
                HomeFragment(),
                HotFragment(),
                CategoryFragment(),
                CartFragment(),
                MineFragment()
        )
        main_radio_group.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rb_main_home -> {
                    changeFragment(currentFragment, framents[0])
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                }
                R.id.rb_main_hot -> {
                    changeFragment(currentFragment, framents[1])
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                }
                R.id.rb_main_category -> {
                    changeFragment(currentFragment, framents[2])
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
                }
                R.id.rb_main_cart -> {
                    changeFragment(currentFragment, framents[3])
                    drawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
                }
                R.id.rb_main_mine -> {
                    changeFragment(currentFragment, framents[4])
                }
            }
        }
        rb_main_home.isChecked = true
    }

    private var currentFragment: Fragment? = null
    /**
     * @param from 当前显示的Fragment, 即将被隐藏
     * @param to   将要切换到的Fragment，即将要显示
     */
    private fun changeFragment(from: Fragment?, to: Fragment?) {
        val ft = childFragmentManager.beginTransaction()
        if (from == to || null == to) return
        currentFragment = to
        if (null != from) ft.hide(from)
        if (to.isAdded) ft.show(to).commit()
        else ft.add(R.id.fl_content_main, to).commit()
    }


    /*  fab.setOnClickListener { view ->
          Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                  .setAction("Action", null).show()
      }

      val toggle = ActionBarDrawerToggle(
              this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
      drawer_layout.addDrawerListener(toggle)
      toggle.syncState()

      nav_view.setNavigationItemSelectedListener(this)
  }

  override fun onBackPressed() {
      if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
          drawer_layout.closeDrawer(GravityCompat.START)
      } else {
          super.onBackPressed()
      }
  }

  override fun onCreateOptionsMenu(menu: Menu): Boolean {
      // Inflate the menu; this adds items to the action bar if it is present.
      menuInflater.inflate(R.menu.main, menu)
      return true
  }

  override fun onOptionsItemSelected(item: MenuItem): Boolean {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      when (item.itemId) {
          R.id.action_settings -> return true
          else -> return super.onOptionsItemSelected(item)
      }
  }

  override fun onNavigationItemSelected(item: MenuItem): Boolean {
      // Handle navigation view item clicks here.
      when (item.itemId) {
          R.id.nav_camera -> {
              // Handle the camera action
          }
          R.id.nav_gallery -> {

          }
          R.id.nav_slideshow -> {

          }
          R.id.nav_manage -> {

          }
          R.id.nav_share -> {

          }
          R.id.nav_send -> {

          }
      }

      drawer_layout.closeDrawer(GravityCompat.START)
      return true
  }*/

}